#include <vector>

using namespace std;

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int num = 0;
        int val = nums[0];
        for (int& it : nums) {
            if (it == val) {
                num++;
            } else {
                if (num == 0) {
                    val = it;
                    num = 1;
                } else {
                    num--;
                }
            }
        }
        return val;
    }
};